package hu.gar.core.dao.impl;

import hu.gar.core.dao.RoleDao;
import hu.gar.core.entity.Role;
import hu.gar.vo.RoleVO;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

@Repository
public class RoleDaoImpl extends GenericDaoImpl<Role, Long, RoleVO> implements
		RoleDao {

	
	private static final long serialVersionUID = 1321598809081635794L;

	@Override
	public Role toEntityImpl(Role entity, RoleVO vo) {
		entity.setRole(vo.getRole());
		return entity;
	}

	@Override
	public RoleVO toVOImpl(RoleVO vo, Role entity) {
		vo.setRole(entity.getRole());
		return vo;
	}

	@Override
	public Role findByName(String name) {
		try {
			Query query = this.em
					.createQuery("select r FROM Role r where r.role= :name");
			query.setParameter("name", name);
			Role role = (Role) query.getSingleResult();

			return role;
		} catch (NoResultException e) {
			return null;
		}
	}

}
